package com.bootdo.app.service.impl;

import com.bootdo.app.dao.AppUserDao;
import com.bootdo.app.exception.AppException;
import com.bootdo.app.exception.RCode;
import com.bootdo.app.htmlVO.customer.MyUserInfo;
import com.bootdo.app.htmlVO.customer.OrderListResult;
import com.bootdo.app.service.AppUserInfoService;
import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.app.utils.returnresponse.ServerResponse;
import com.bootdo.appsystem.domain.OrderproductDO;
import com.bootdo.appsystem.domain.UseraccountDO;
import com.bootdo.appsystem.service.UseraccountService;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.utils.FileType;
import com.bootdo.common.utils.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 【@author 】 @fan
 * 【@Email  】 mooncluyed@outlook.com
 * 【类解释  】
 * 【@Date   】 2018/5/12 0012
 **/
@Service
public class AppUserInfoServiceImpl implements AppUserInfoService{

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppUserDao appUserDao;
    @Autowired
    private BootdoConfig bootdoConfig;
    @Autowired
    private AppUserInfoService appUserInfoService;
    @Autowired
    private UseraccountService useraccountService;


    @Override
    public MyUserInfo getMyUserInfo(UseraccountDO useraccountDO) {
        return null;
    }

    @Override
    public List<OrderproductDO> getOrderList(UseraccountDO useraccountDO) {
        return null;
    }



    @Override
    public FileDO uploadUserImage(MultipartFile file, UseraccountDO useraccountDO) {
        String fileName = file.getOriginalFilename();
        fileName = FileUtil.renameToUUID(fileName);
        //创建新的文件夹
        //创建格式：/files/seller/seller1/20180514/201805149f751de8-2707-42d3-891c-b888c1f360c8.jpg
        File file2 = new File(bootdoConfig.getUploadPath()+"/user/" + "user" + useraccountDO.getUid() + "/" + new SimpleDateFormat("yyyyMMdd").format(new Date()));
        FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/user/" + "user"+ useraccountDO.getUid() + "/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/"+ file2.getName()+fileName, new Date());
        try {
            FileUtil.uploadFile(file.getBytes(), file2.getPath()+ "\\" , file2.getName()+fileName);
        } catch (Exception e) {
            logger.error("【上传接口出现错误】" + e.getMessage());
            throw new AppException(RCode.FANHUINULL);
        }
        return sysFile;
    }

    @Override
    public ServerResponse<MyUserInfo> getMyUserInfo() {
        MyUserInfo myUserInfo = new MyUserInfo();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        UseraccountDO useraccountDO = (UseraccountDO) request.getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);

        //获取最新的用户数据，不从数据库中取出
        UseraccountDO newsUser = useraccountService.get(useraccountDO.getUid());
        newsUser.setPassword(null);
        newsUser.setPayword(null);
        myUserInfo.setUseraccountDO(newsUser);

        Integer ordercount = 0;

        List<OrderListResult> orderListResultList = appUserDao.getUserOrderInfo(useraccountDO.getUid());
        for (OrderListResult orderListResult : orderListResultList){
            ordercount += orderListResult.getCountnumber();
            switch (orderListResult.getoStatus()){
                case 0 : myUserInfo.setWeifukuan(orderListResult.getCountnumber());break;
                case 1 : myUserInfo.setYifukuan(orderListResult.getCountnumber());break;
                case 2 : myUserInfo.setYifahuo(orderListResult.getCountnumber());break;
                case 3 : myUserInfo.setDaipingjia(orderListResult.getCountnumber());break;
                case 4 : myUserInfo.setYiwancheng(orderListResult.getCountnumber());break;
            }
        }

        Integer userCount = appUserDao.getUserOneCount(useraccountDO.getUid()) + appUserDao.getUserTwoCount(useraccountDO.getUid());
        myUserInfo.setTuiguangCount(userCount);
        myUserInfo.setOrdercount(ordercount);

        return ServerResponse.createBySuccess(myUserInfo);
    }


}
